<?php

namespace Addons\OaSystem\Controller;
use Think\ManageBaseController;

class OaEmpAttendanceController extends OaBaseController
{
    var $model;

    function _initialize()
    {
        $this->model = $this->getModel('oa_emp_attendance');
        
        parent::_initialize();
        $controller = strtolower(CONTROLLER_NAME);
        $action = strtolower(ACTION_NAME);

        $res ['title'] = '考勤信息';
        $res ['url'] = addons_url('OaSystem://OaEmpAttendance/attendancelists', array('mdm' => I('mdm')));
        $res ['class'] = $action == 'attendancelists' ? 'cur' : '';
        $nav [] = $res;
        $this->assign('nav', $nav);

        $res ['title'] = '考勤列表';
        $sub_nav[] = $res;
        $res ['title'] = '加班列表';
        $res ['url'] = addons_url('OaSystem://OaEmpAttendance/overtimelists', array('mdm' => I('mdm')));
        $res ['class'] = $action == 'overtimelists' ? 'cur' : '';
        $sub_nav[] = $res;
        $res ['title'] = '请假列表';
        $res ['url'] = addons_url('OaSystem://OaEmpAttendance/leavelists', array('mdm' => I('mdm')));
        $res ['class'] = $action == 'leavelists' ? 'cur' : '';
        $sub_nav[] = $res;
        $res ['title'] = '考勤点管理';
        $res ['url'] = addons_url('OaSystem://OaCheckPoint/lists', array('mdm' => I('mdm')));
        $res ['class'] = $controller == 'oacheckpoint' ? 'cur' : '';
        $sub_nav[] = $res;
        $res ['title'] = '工作时间设置';
        $res ['url'] = addons_url('OaSystem://OaWorkTimeSetting/lists', array('mdm' => I('mdm')));
        $res ['class'] = $controller == 'oaworktimesetting' ? 'cur' : '';
        $sub_nav[] = $res;
        $this->assign('sub_nav', $sub_nav);
    }

    function attendancelists()
    {
        if(IS_POST) {
//            if(empty($_POST['emp_name'])) {
//                $this->error('请输入查询姓名!');
//                return;
//            }
            $querypara = json_encode($_POST);
            redirect(U('attendancelists', array('query'=>$querypara)));
        }
        $p = empty(I('p')) ? 1 : I('p');
        $row = 20;
        $querypara = json_decode($_GET['query'], true);
        if(empty($querypara)) {
            $y_start = $y_end = date('Y');
            $m_end = date('m');
            $m_start = $m_end - 1;
            $dept_id = 0;
            $emp_name = null;
            $type = 0;
        } else {
            $y_start = $querypara['year_start'];
            $y_end = $querypara['year_end'];
            $m_start = $querypara['month_start'];
            $m_end = $querypara['month_end'];
            $dept_id = $querypara['dept_id'];
            $emp_name = $querypara['emp_name'];
            $type = $querypara['type'];

            $query_start = strtotime($y_start.'-'.$m_start.'-01 00:00:00');
            $m_days = cal_days_in_month(CAL_GREGORIAN, $m_end, $y_end);
            $query_end = strtotime($y_end.'-'.$m_end.'-'.$m_days.' 23:59:59');
            $now = time();
            if($query_end > $now) $query_end = $now;

            $emp_ids = $this->getEmpIds($emp_name, $dept_id);
            $i = 0;
            switch($type) {
                case 0:
                    $len_date = (int)(($query_end - $query_start)/86400) + 1;
                    $pages_peremp = (int)($len_date/$row) + (($len_date%$row==0)?0:1);
                    $len = $pages_peremp*$row*count($emp_ids);
                    $p_emp = (($p%$pages_peremp)==0)?$pages_peremp:($p%$pages_peremp);
                    $query_start += ($p_emp-1)*$row*86400;
                    $tmp_end = $query_start + $row*86400 - 1;
                    $query_end = ($query_end > $tmp_end) ? $tmp_end : $query_end;
                    $emp_index = (int)($p/$pages_peremp) + (($p%$pages_peremp==0)?0:1) - 1;
                    $emp_id = $emp_ids[$emp_index];

                    $attendances = $this->getAttendances($query_start, $query_end, $emp_id);
                    for($t=$query_start; $t<$query_end; $t+=86400,$i++) {
                        $empInfo = $this->getUserInfo($emp_id);
                        $data[$i]['name'] =  $empInfo['name'];
                        $data[$i]['dept'] = $empInfo['dept'];
                        $data[$i]['date'] = date('Y-m-d', $t);
                        $data[$i]['day_type'] = $this->getDayType($t);
                        $data[$i]['punchtime'] = $attendances[$t]['timestr'];
                        $data[$i]['leave_hours'] = $this->getLeaveTime($t, $emp_id);
                        $data[$i]['over_hours'] = $this->getWorkOverTime($t, $emp_id);
                        $data[$i]['remark'] = $attendances[$t]['remark'];
                        if(!empty($data[$i]['leave_hours']))
                            $data[$i]['remark'] = '休假';
                    }
                    break;
                case 1:
                case 2:
                case 3:
                    foreach ($emp_ids as $e) {
                        $empInfo = $this->getUserInfo($e);
                        $attendances = $this->getAttendances($query_start, $query_end, $e);
                        foreach ($attendances as $t => $a) {
                            if ($a['type'] == $type || $a['type'] == 3) {
                                $data[$i]['name'] = $empInfo['name'];
                                $data[$i]['dept'] = $empInfo['dept'];
                                $data[$i]['date'] = date('Y-m-d', $t);
                                $data[$i]['day_type'] = $this->getDayType($t);
                                $data[$i]['punchtime'] = $a['timestr'];
                                $data[$i]['leave_hours'] = $this->getLeaveTime($t, $e);
                                $data[$i]['over_hours'] = $this->getWorkOverTime($t, $e);
                                $data[$i]['remark'] = $a['remark'];
                                $i++;
                            }
                        }
                    }
                    $len = $i;
                    $tmp = array_chunk($data, $row);
                    $data = $tmp[$p-1];
                    break;

                case 4:
                    if($p == 1) {
                        S('attendances_data', NULL);
                    } else {
                        $attendances_data = S('attendances_data');
                        if($attendances_data != NULL) {
                            $data = $attendances_data;
                            $len = count($data);
                            $tmp = array_chunk($data, $row);
                            $data = $tmp[$p-1];
                            break;
                        }
                    }
                    foreach ($emp_ids as $e) {
                        $empInfo = $this->getUserInfo($e);
                        $attendances = $this->getAttendances($query_start, $query_end, $e);
                        for($t=$query_start; $t<$query_end; $t+=86400) {
                            if(empty($attendances[$t])) {
                                if($this->getDayType($t) != '工作日')
                                    continue;
                                if($this->hasApplyLeaveForThisDay($t, $e))
                                    continue;
                                $data[$i]['name'] =  $empInfo['name'];
                                $data[$i]['dept'] = $empInfo['dept'];
                                $data[$i]['date'] = date('Y-m-d', $t);
                                $data[$i]['day_type'] = $this->getDayType($t);
                                $data[$i]['leave_hours'] = $this->getLeaveTime($t, $e);
                                $data[$i]['remark'] = '未打卡';
                                $i++;
                            } else if($attendances[$t]['type']==4) {
                                $data[$i]['name'] = $empInfo['name'];
                                $data[$i]['dept'] = $empInfo['dept'];
                                $data[$i]['date'] = date('Y-m-d', $t);
                                $data[$i]['day_type'] = $this->getDayType($t);
                                $data[$i]['punchtime'] = $attendances[$t]['timestr'];
                                $data[$i]['leave_hours'] = $this->getLeaveTime($t, $e);
                                $data[$i]['over_hours'] = $this->getWorkOverTime($t, $e);
                                $data[$i]['remark'] = '缺打卡';
                                $i++;
                            }
                        }
                    }
                    S('attendances_data', $data, 1800);
                    $len = $i;
                    $tmp = array_chunk($data, $row);
                    $data = $tmp[$p-1];
                    break;
            }

        }

        for($y = 2018; $y <= 2030; $y++) {
            $query['date']['ystart'][$y]['id'] = $y;
            $query['date']['ystart'][$y]['name'] = $y;
            $query['date']['yend'][$y]['id'] = $y;
            $query['date']['yend'][$y]['name'] = $y;
        }
        for($m = 1; $m <= 12; $m++) {
            $query['date']['mstart'][$m]['id'] = $m;
            $query['date']['mstart'][$m]['name'] = $m;
            $query['date']['mend'][$m]['id'] = $m;
            $query['date']['mend'][$m]['name'] = $m;
        }
        $query['date']['ystart'][$y_start]['selected'] = 'selected';
        $query['date']['yend'][$y_end]['selected'] = 'selected';
        $query['date']['mstart'][$m_start]['selected'] = 'selected';
        $query['date']['mend'][$m_end]['selected'] = 'selected';

        $depts = $this->getDepts();
        foreach ($depts as $i => $d) {
            if($dept_id == $d['id'])
                $depts[$i]['selected'] = 'selected';
        }

        $types = array(
            array('id'=>0, 'name'=>'全部考勤'),
            array('id'=>1, 'name'=>'迟到'),
            array('id'=>2, 'name'=>'早退'),
            array('id'=>3, 'name'=>'迟到早退'),
            array('id'=>4, 'name'=>'未打卡'),
        );
        foreach ($types as $i => $t) {
            if($type == $t['id'])
                $types[$i]['selected'] = 'selected';
        }

        $query['depts'] = $depts;
        $query['emp_name'] = $emp_name;
        $query['types'] = $types;

        if ($len > $row) {
            $page = new \Think\Page ( $len, $row );
            $page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
            $list_data['_page'] = $page->show ();
        }
        $this->assign('post_url',addons_url('OaSystem://OaEmpAttendance/attendancelists', array('mdm' => I('mdm'))));
        $this->assign('_page',$list_data['_page']);
        $this->assign('list_data', $data);
        $this->assign('query', $query);
        $this->assign('attendances', $attendances);
        $this->display('attendancelists');
    }

    function overtimelists()
    {
        if(IS_POST) {
            $querypara = json_encode($_POST);
            redirect(U('overtimelists', array('query'=>$querypara)));
        }

        $p = empty(I('p')) ? 1 : I('p');
        $row = 20;
        $querypara = json_decode($_GET['query'], true);
        if(empty($querypara)) {
            $y_start = $y_end = date('Y');
            $m_end = date('m');
            $m_start = $m_end - 1;
            $dept_id = 0;
            $level_id = 0;
            $emp_name = null;
        } else {
            $y_start = $querypara['year_start'];
            $y_end = $querypara['year_end'];
            $m_start = $querypara['month_start'];
            $m_end = $querypara['month_end'];
            $dept_id = $querypara['dept_id'];
            $level_id = $querypara['level_id'];
            $emp_name = $querypara['emp_name'];
        }

        $query_start = strtotime($y_start.'-'.$m_start.'-01 00:00:00');
        $m_days = cal_days_in_month(CAL_GREGORIAN, $m_end, $y_end);
        $query_end = strtotime($y_end.'-'.$m_end.'-'.$m_days.' 23:59:59');
//        $now = time();
//        if($query_end > $now) $query_end = $now;

        $map['type'] = 1;
        $map['is_del'] = 0;
        $map['status'] = 1;

        if(!empty($emp_name)) {
            if(($emp_id = $this->getEmpId($emp_name, $dept_id)) == false)
                return false;
            $map['applyer'] = $emp_id;
        } else {
            if(!empty($dept_id)) {
                $emp_map['dept_id'] = $dept_id;
            }
            if(!empty($level_id)) {
                $duty_map['level'] = ($level_id>10)?$level_id:(array('elt', 10));
                $dutys = db_select('oa_duty', $duty_map);
                $dutyids = array_column($dutys,'id');
                $emp_map['duty_id'] = array('in', $dutyids);
            }
            $emps = db_select('oa_employee', $emp_map);
            $empids = array_column($emps,'id');
            $map['applyer'] = array('in', $empids);
        }
        $map['start_time'] = array(array('egt', $query_start), array('elt', $query_end));

        $len = M('oa_application')->where($map)->count();
        $ot_applys = M('oa_application')->where($map)->order('submit_date desc')->page($p,$row)->select();
        foreach ($ot_applys as $i => $v) {
            $empInfo = $this->getUserInfo($v['applyer']);
            $data[$i]['name'] = $empInfo['name'];
            $data[$i]['dept'] = $empInfo['dept'];
            $data[$i]['submit_date'] = date('Y-m-d', $v['submit_date']);
            $data[$i]['start_time'] = date('y/m/d H:i', $v['start_time']);
            $data[$i]['end_time'] = date('y/m/d H:i', $v['end_time']);
            $data[$i]['description'] = $v['description'];
        }

        for($y = 2018; $y <= 2030; $y++) {
            $query['date']['ystart'][$y]['id'] = $y;
            $query['date']['ystart'][$y]['name'] = $y;
            $query['date']['yend'][$y]['id'] = $y;
            $query['date']['yend'][$y]['name'] = $y;
        }
        for($m = 1; $m <= 12; $m++) {
            $query['date']['mstart'][$m]['id'] = $m;
            $query['date']['mstart'][$m]['name'] = $m;
            $query['date']['mend'][$m]['id'] = $m;
            $query['date']['mend'][$m]['name'] = $m;
        }
        $query['date']['ystart'][$y_start]['selected'] = 'selected';
        $query['date']['yend'][$y_end]['selected'] = 'selected';
        $query['date']['mstart'][$m_start]['selected'] = 'selected';
        $query['date']['mend'][$m_end]['selected'] = 'selected';

        $depts = $this->getDepts();
        foreach ($depts as $i => $d) {
            if($dept_id == $d['id'])
                $depts[$i]['selected'] = 'selected';
        }

        $levels = $this->getEmpLevelList();
        foreach ($levels as $i => $d) {
            if($level_id == $d['id'])
                $levels[$i]['selected'] = 'selected';
        }

        $query['depts'] = $depts;
        $query['levels'] = $levels;
        $query['emp_name'] = $emp_name;

        if ($len > $row) {
            $page = new \Think\Page ( $len, $row );
            $page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
            $list_data['_page'] = $page->show ();
        }
        $this->assign('_page',$list_data['_page']);
        $this->assign('list_data', $data);
        $this->assign('query', $query);
        $this->display();
    }

    function leavelists()
    {
        if(IS_POST) {
            $querypara = json_encode($_POST);
            redirect(U('leavelists', array('query'=>$querypara)));
        }

        $p = empty(I('p')) ? 1 : I('p');
        $row = 20;
        $querypara = json_decode($_GET['query'], true);
        if(empty($querypara)) {
            $y_start = $y_end = date('Y');
            $m_end = date('m');
            $m_start = $m_end - 1;
            $dept_id = 0;
            $level_id = 0;
            $subtype_id = 0;
            $emp_name = null;
        } else {
            $y_start = $querypara['year_start'];
            $y_end = $querypara['year_end'];
            $m_start = $querypara['month_start'];
            $m_end = $querypara['month_end'];
            $dept_id = $querypara['dept_id'];
            $level_id = $querypara['level_id'];
            $subtype_id = $querypara['subtype_id'];
            $emp_name = $querypara['emp_name'];
        }

        $query_start = strtotime($y_start.'-'.$m_start.'-01 00:00:00');
        $m_days = cal_days_in_month(CAL_GREGORIAN, $m_end, $y_end);
        $query_end = strtotime($y_end.'-'.$m_end.'-'.$m_days.' 23:59:59');
//        $now = time();
//        if($query_end > $now) $query_end = $now;

        $map['type'] = 2;
        $map['is_del'] = 0;
        $map['status'] = 1;
        if(!empty($subtype_id)) {
            $threeDays = 3*24*60*60;
            if($subtype_id == 1) {
                $map['total_money'] = array('elt', $threeDays);
            } else if($subtype_id == 2) {
                $map['total_money'] = array('gt', $threeDays);
            }
        }

        if(!empty($emp_name)) {
            if(($emp_id = $this->getEmpId($emp_name, $dept_id)) == false)
                return false;
            $map['applyer'] = $emp_id;
        } else {
            if(!empty($dept_id)) {
                $emp_map['dept_id'] = $dept_id;
            }
            if(!empty($level_id)) {
                $duty_map['level'] = ($level_id>10)?$level_id:(array('elt', 10));
                $dutys = db_select('oa_duty', $duty_map);
                $dutyids = array_column($dutys,'id');
                $emp_map['duty_id'] = array('in', $dutyids);
            }
            $emps = db_select('oa_employee', $emp_map);
            $empids = array_column($emps,'id');
            $map['applyer'] = array('in', $empids);
        }
        $map['start_time'] = array(array('egt', $query_start), array('elt', $query_end));

        $len = M('oa_application')->where($map)->count();
        $ot_applys = M('oa_application')->where($map)->order('submit_date desc')->page($p,$row)->select();
        foreach ($ot_applys as $i => $v) {
            $empInfo = $this->getUserInfo($v['applyer']);
            $data[$i]['name'] = $empInfo['name'];
            $data[$i]['dept'] = $empInfo['dept'];
            $data[$i]['submit_date'] = date('Y-m-d', $v['submit_date']);
            $data[$i]['start_time'] = date('y/m/d H:i', $v['start_time']);
            $data[$i]['end_time'] = date('y/m/d H:i', $v['end_time']);
            $data[$i]['description'] = $v['description'];
        }

        for($y = 2018; $y <= 2030; $y++) {
            $query['date']['ystart'][$y]['id'] = $y;
            $query['date']['ystart'][$y]['name'] = $y;
            $query['date']['yend'][$y]['id'] = $y;
            $query['date']['yend'][$y]['name'] = $y;
        }
        for($m = 1; $m <= 12; $m++) {
            $query['date']['mstart'][$m]['id'] = $m;
            $query['date']['mstart'][$m]['name'] = $m;
            $query['date']['mend'][$m]['id'] = $m;
            $query['date']['mend'][$m]['name'] = $m;
        }
        $query['date']['ystart'][$y_start]['selected'] = 'selected';
        $query['date']['yend'][$y_end]['selected'] = 'selected';
        $query['date']['mstart'][$m_start]['selected'] = 'selected';
        $query['date']['mend'][$m_end]['selected'] = 'selected';

        $depts = $this->getDepts();
        foreach ($depts as $i => $d) {
            if($dept_id == $d['id'])
                $depts[$i]['selected'] = 'selected';
        }
        $levels = $this->getEmpLevelList();
        foreach ($levels as $i => $d) {
            if($level_id == $d['id'])
                $levels[$i]['selected'] = 'selected';
        }

        $subtypes = array(
            array('id'=>0, 'name'=>'全部'),
            array('id'=>1, 'name'=>'三天以内'),
            array('id'=>2, 'name'=>'三天以上')
        );
        foreach ($subtypes as $i => $d) {
            if($subtype_id == $d['id'])
                $subtypes[$i]['selected'] = 'selected';
        }

        $query['depts'] = $depts;
        $query['levels'] = $levels;
        $query['subtypes'] = $subtypes;
        $query['emp_name'] = $emp_name;

        if ($len > $row) {
            $page = new \Think\Page ( $len, $row );
            $page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
            $list_data['_page'] = $page->show ();
        }
        $this->assign('_page',$list_data['_page']);
        $this->assign('list_data', $data);
        $this->assign('query', $query);
        $this->display();
    }

    private function getEmpId($emp_name, $dept_id) {
        if(!empty($dept_id)) $map['dept_id'] = $dept_id;
        $map['name'] = $emp_name;
        if(($emp = db_find('oa_employee', $map)) == NULL) {
            $this->error('找不到该查询姓名的员工信息,请检查部门或姓名!');
            return false;
        }
        return $emp['id'];
    }

    private function getEmpIds($emp_name, $dept_id) {
        $map['appid'] = $this->appId;
        if(!empty($emp_name)) $map['name'] = $emp_name;
        if(!empty($dept_id)) {
            $deptids = array();
            $dept = db_find('oa_dept', ['id'=>$dept_id]);
            if($dept['dept_grade_id'] == 2) {
                $subdepts = db_select('oa_dept', ['pid'=>$dept_id]);
                if($subdepts != NULL) {
                    $deptids = array_column($subdepts, 'id');
                }
            }
            array_push($deptids, $dept_id);
            $map['dept_id'] = array('in', $deptids);
        }
        if(($emps = db_select('oa_employee', $map)) == NULL) {
            $this->error('找不到该查询条件的员工信息,请检查部门或姓名!');
            return false;
        }
        $empids = array_column($emps, 'id');
        return $empids;
    }

    private function getAttendances($date_start, $date_end, $emp_id) {
        $map['date'] = array(array('egt', $date_start), array('elt', $date_end));
        if(!empty($emp_id))
            $map['emp_id'] = $emp_id;
        $attendances = db_select('oa_emp_attendance', $map, 'date asc');
        foreach ($attendances as $a) {
            $time = ($a['type']%2==1 && !empty($a['time2nd'])) ? $a['time2nd'] : $a['time'];
            $attLists[$a['date']]['time'][$a['type']] = $time;
            $attLists[$a['date']]['timestr'][$a['type']] = date('H:i', $time);
        }

        $clockon = $this->workTime['clockon'];
        $time_buf = 60;
        foreach ($attLists as $t => $v) {
            if(($v['time'][0] > ($clockon[0]+$time_buf)) ||
                ($v['time'][2] > ($clockon[2]+$time_buf))) {
                $attLists[$t]['remark'] = '迟到';
                $attLists[$t]['type'] = 1;
            }
            if(($v['time'][1] < ($clockon[1]-$time_buf)) ||
                ($v['time'][3] < ($clockon[3]-$time_buf))) {
                $attLists[$t]['remark'] .= '早退';
                $attLists[$t]['type'] += 2;
            }
            if(empty($v['time'][0]) || empty($v['time'][3])) {
                $attLists[$t]['remark'] = '缺打卡';
                $attLists[$t]['type'] = 4;
            }
        }
        return $attLists;
    }

    private function getDayType($dayTs) {
        $holidays = $this->workTime['holidays'];
        $adjust_rest_days = $this->workTime['restdays'];
        $adjust_work_days = $this->workTime['workdays'];
        if(in_array($dayTs, $holidays))
            return '节假日';
        if(in_array($dayTs, $adjust_rest_days))
            return '休息日';
        if(in_array($dayTs, $adjust_work_days))
            return '工作日';
        $dayofweek = date('w', $dayTs);
        if($dayofweek >= 1 && $dayofweek <= 5)
            return '工作日';
        else
            return '休息日';
    }

    private function getLeaveApply($start_time, $end_time, $emp_id)
    {
        $map['applyer'] = $emp_id;
        $map['type'] = 2;
        $map['is_del'] = 0;
        $map['status'] = 1;
        $tmp_map['start_time'] = array(array('egt', $start_time), array('elt', $end_time));
        $tmp_map['end_time'] = array(array('egt', $start_time), array('elt', $end_time));
        $tmp_map['_logic'] = 'or';
        $map['_complex'] = $tmp_map;
        $leaveApply = db_select('oa_application', $map);

        return $leaveApply;
    }
    private function getLeaveTime($dayTs, $emp_id) {
        $clock_on = $this->workTime['clockon'];
        $work_hour = $dayTs + $clock_on[0];
        $off_hour = $dayTs + end($clock_on);

        $leaveApply = $this->getLeaveApply($work_hour, $off_hour, $emp_id);

        $leave_des = null;
        foreach ($leaveApply as $la) {
            $leave_start = ($la['start_time'] > $work_hour) ? $la['start_time'] : $work_hour;
            $leave_end = ($la['end_time'] > $off_hour) ? $off_hour : $la['end_time'];
            $leave_des .= date('H:i', $leave_start).' - '.date('H:i', $leave_end).'<br>';
        }
        return $leave_des;
    }
    private function hasApplyLeaveForThisDay($dayTs, $emp_id) {
        $clock_on = $this->workTime['clockon'];
        $work_hour = $dayTs + $clock_on[0];
        $off_hour = $dayTs + end($clock_on);

        $leaveApply = $this->getLeaveApply($work_hour, $off_hour, $emp_id);
        return ($leaveApply != NULL);
    }

    private function getWorkOverTime ($dayTs, $emp_id) {
        $clock_on = $this->workTime['clockon'];
        $work_hour = $dayTs + $clock_on[0];
        $off_hour = $dayTs + end($clock_on);

        $map['applyer'] = $emp_id;
        $map['type'] = 1;
        $map['is_del'] = 0;
        $map['status'] = 1;
        $map['start_time'] = array('egt', $off_hour);
        if(($OverWorkApply = db_select('oa_application', $map)) == NULL) {
            return null;
        }
        $des = '';
        $realHour = 0;
        foreach ($OverWorkApply as $la) {
            $applyOverWorkhour = ($la['end_time'] - $la['start_time'])/60/60;
            $realClockOn = M('OaEmpAttendance')->where(array('type'=>3,'emp_id'=>$emp_id,'date'=>$dayTs))->find();
            $realHour = ((empty($realClockOn)?0:(empty($realClockOn['time2nd'])?$realClockOn['time']:$realClockOn['time2nd'])) - $la['start_time'])/60/60;
            if ($realHour < 0) {
                $realHour = 0;
            }
        }
        // $des .= '申请'.$applyOverWorkhour.'小时,实际加班'.$realHour.'小时<br>';
        $des .= '加班'.$realHour.'小时<br>';
        return $des;
    }

    public function getAttendanceReport () {
        $type = empty(I('type')) ? 0: I('type');
        switch ($type) {
            case 3 :
                break;
            default:
                break;
        }
    }

    private function getEmpLevelList() {
        $levelDuty = array(
            '副关长' => '副关长',
            '处级干部' => '财务处_处长',
            '副处级干部' => '财务处_副处长',
            '科级干部' => '财务处_经费科_科长',
            '副科级干部' => '财务处_经费科_副科长',
        );
        $levellists = array();
        array_push($levellists, array('id'=>0, 'name'=>'全部'));
        foreach ($levelDuty as $k => $v) {
            $duty = db_find('oa_duty', ['name'=>$v]);
            $level['id'] = $duty['level'];
            $level['name'] = $k;
            array_push($levellists, $level);
        }
        array_push($levellists, array('id'=>10, 'name'=>'其他人员'));
        return $levellists;
    }
}